//
//  JPSecondViewController.m
//  用户信息设置
//
//  Created by 周述坚 on 16/11/13.
//  Copyright © 2016年 周述坚. All rights reserved.
//

#import "JPSecondViewController.h"
#import "JPInformationModel.h"

@interface JPSecondViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (strong,nonatomic)NSArray *information;
@property (strong,nonatomic)NSArray *cities;
@property(nonatomic, assign) NSInteger currentSelectedProvice;
@property (nonatomic,copy)NSString *privance;

@end

@implementation JPSecondViewController {
    NSString *_ge;
    NSString *_shi;
    NSString *_bai ;
}

/**
 *  懒加载
 */
- (NSArray *)information {
    if (_information == nil) {
        
        if (self.indexPath.row == 6 || self.indexPath.row == 7) {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"province.plist" ofType:nil];
            NSArray *arr = [NSArray arrayWithContentsOfFile:path];
            self.cities = arr;
            
            NSMutableArray *arrM = [NSMutableArray array];
            for (NSDictionary *dict in arr) {
                // 省
                JPInformationModel *model = [JPInformationModel informationModelWithDict:dict];
                [arrM addObject:model];
            }
            _information = arrM;
        } else {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"Height.plist" ofType:nil];
            _information = [NSArray arrayWithContentsOfFile:path];
        }
    }
    return _information;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentSelectedProvice = 0;
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    _ge = [NSMutableString stringWithFormat:@"0"];
    _shi = [NSMutableString stringWithFormat:@"0"];
    _bai = [NSMutableString stringWithFormat:@"0"];
}

- (IBAction)okAction:(id)sender {
    if (self.isAction != 3) {
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    if (self.completion != nil) {
        NSString *name = self.value;
        self.completion(name);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark pickerViewDataSource

/**
 *  返回多少组
 */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    if (self.indexPath.row == 6 || self.indexPath.row == 7) {
        return 2;
    } else {
        return 3;
    }
}

/**
 *  每一组有多少行
 */
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if(self.indexPath.row == 6 || self.indexPath.row == 7) {
        if (component == 0) {
            return self.information.count;
        }else{
            JPInformationModel *jpModel = self.information[self.currentSelectedProvice];
            return jpModel.cities.count;
        }
    } else {
        return [self.information[component] count];
    }
}

#pragma mark pickerViewDelegate

/**
 *  每行显示什么
 */
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    _isAction = 1;
    if(self.indexPath.row == 6 || self.indexPath.row == 7) {
        if (component == 0) {
            JPInformationModel *jpModel = self.information[row];
            return jpModel.name;
        }else{
            JPInformationModel *jpModel = self.information[self.currentSelectedProvice];
            return jpModel.cities[row];
        }
    } else {
        return self.information[component][row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.isAction = 3;
    if(self.indexPath.row == 6 || self.indexPath.row == 7) {
        if (component == 0) {
            self.currentSelectedProvice = row;
            [self.pickerView reloadComponent:1];
            JPInformationModel *jpModel = self.information[row];
            self.address = [NSString stringWithFormat:@"%@",jpModel.name];
        } else {
            JPInformationModel *jpModel = self.information[self.currentSelectedProvice];
            self.value = [NSString stringWithFormat:@"%@-%@",self.address,jpModel.cities[row]];
        }
    } else {
        NSString *dataStr = self.information[component][row];
        if (component == 0) {
            _bai = dataStr;
            if ([_bai isEqualToString:@"0"]) {
                _bai = @"";
            }
            self.value = [NSString stringWithFormat:@"%@%@%@",_bai,_shi,_ge].copy;
            
        }else if(component == 1) {
            _shi = dataStr;
            self.value = [NSString stringWithFormat:@"%@%@%@",_bai, _shi,_ge].copy;
            
        } else {
            _ge = dataStr;
            self.value = [NSString stringWithFormat:@"%@%@%@",_bai,_shi,_ge].copy;
            
        }
        if ([_bai isEqualToString:@"0"]) {
            _bai = @"";
        }
        self.value = [NSString stringWithFormat:@"%@%@%@",_bai,_shi,_ge];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
